package com.wenwan.manage.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wenwan.manage.mapper.GiveGetVolumeShareMapper;
import com.wenwan.manage.service.GiveGetService;
import com.wenwan.manage.service.GiveGetVolumeShareService;
import com.wenwan.manage.setting.RegExpSetting;
import com.wenwan.manage.setting.StatusSetting;
import com.wenwan.manage.util.Util;

@Service
public class GiveGetVolumeShareServiceImpl implements GiveGetVolumeShareService {

	@Autowired
	private GiveGetVolumeShareMapper giveGetVolumeShareMapper;

	@Autowired
	private GiveGetService giveGetService;

	@Override
	public Map<String, Object> create(String giveGetId, String userId) {
		// TODO Auto-generated method stub
		// DataToObject<Boolean> dto = new DataToObject<Boolean>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (giveGetId != null && !giveGetId.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "giveGetId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("giveGetId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (userId != null && !userId.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "userId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("userId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		String id = DigestUtils.md5Hex(giveGetId + userId).toUpperCase();
		map.put("id", id);
		map.put("createTime", Util.getTime());
		map.put("updateTime", Util.getTime());
		map.put("giveGetId", giveGetId);
		map.put("userId", userId);

		// 如果不存在，则
		if (!(Boolean) isExist(id).get("data")) {
			// +1
			giveGetService.update(giveGetId, null, null, null, null, null, null, 1, null, null);
			// 保存到数据库
			Boolean data = giveGetVolumeShareMapper.create(map);
			// dto.setData(data);
			dto.put("data", data);
			dto.put("msg", StatusSetting.MSG_200);
			dto.put("code", StatusSetting.CODE_200);
			return dto;
		} else {

			dto.put("data", false);
			dto.put("msg", "你已经分享过了");
			dto.put("code", "10060");
			// dto.setData(false);
			// dto.setMsg("你已经分享过了");
			// dto.setCode("10060");
			return dto;
		}
	}

	@Override
	public Map<String, Object> isExist(String id) {

		// DataToObject<Boolean> dto = new DataToObject<Boolean>();
		Map<String, Object> dto = new HashMap<String, Object>();
		if (id != null && !id.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);

		Integer count = giveGetVolumeShareMapper.isExist(map);

		if (count == 0) {
			// dto.setData(false);
			dto.put("data", false);
			dto.put("msg", StatusSetting.MSG_200);
			dto.put("code", StatusSetting.CODE_200);
			return dto;
		} else {
			// dto.setData(true);
			dto.put("data", true);
			dto.put("msg", StatusSetting.MSG_200);
			dto.put("code", StatusSetting.CODE_200);
			return dto;
		}
	}

}
